<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <script src="../../js/load.js"></script>
</head>

<body>
<div id="container">
    <div id="formcontainer">
        <form class="layui-form layui-form-pane" id="editform" lay-filter="editform">
            <div class="layui-row">
                <div class="layui-col-xs12">
                    <div class="layui-form-item">
                        <label class="layui-form-label">文件路径</label>
                        <div class="layui-input-block">
                            <input type="text" id="preFilePath" autocomplete="off" disabled class="layui-input required">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-row">
                <div class="layui-col-xs12">
                    <div class="layui-form-item">
                        <label class="layui-form-label">部署状态</label>
                        <div class="layui-input-block">
                            <input type="text" id="status" autocomplete="off" disabled class="layui-input required">
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <div id="opcontainer">
        <input type="file" name="file" id="file" style="display:none" accept=".jar,.war" style="display:none" onchange="HandleRealUpload()" />
        <button class="layui-btn" onclick="Upload()" id="Upload">上传</button>
        <button class="layui-btn layui-btn-danger" onclick="Stop()" id="Stop">停止</button>
        <button class="layui-btn" onclick="Start()" id="Start">替换启动</button>
        <button class="layui-btn layui-btn-primary" onclick="HandleClose()">关闭</button>
    </div>
</div>
</body>
<script>
    var FormDetail = null;
    $(function () {
        LoadDetail();
    })

    function LoadDetail() {
        var param = {
            projectID: GetUrlParam("projectID")
        }
        HttpPost(JSON.stringify(param), "api/depoly/getProjectInfo", function (result) {
            if (result.code == 0) {
                $("#preFilePath").val(result.data.preFilePath)
                $("#status").val(result.data.status)
                InitState();
            }
        })
    }

    function InitState(){
       var preFilePath = $("#preFilePath").val();
       var status = $("#status").val();
       if(preFilePath=='未上传'){
           $("#preFilePath").css('color','red')
       }else{
           $("#preFilePath").css('color','green')
       }
       if(status=='运行中'){
           $("#status").css('color','green')
           $("#Stop").show();
       }else{
           $("#Stop").hide();
       }
       if(status=='已停止'){
           $("#status").css('color','red')
           if(preFilePath!='未上传'){
              $("#Start").show();
           }else{
               $("#Start").hide();
           }
       }else{
           $("#Start").hide();
       }
    }

    function Upload(){
        $("#file").click();
    }

    function HandleRealUpload() {
        var file = document.getElementById("file").files[0];
        var filesize = file.size;
        var filename = file.name;
        var chunkSize = 5 * 1024 * 1024
        var chunkCount = Math.ceil(filesize / chunkSize);
        var worker = new Worker('../../js/worker.js');
        var param = {
            projectID: GetUrlParam("projectID"),
            filename: filename,
            filesize:filesize,
            chunkCount: chunkCount,
            chunkSize:chunkSize,
            file: file,
            uploadUrl: GetRootPath() + "api/depoly/uploadpre"
        }

        var element = layui.element;
        var progressLayer = layer.open({
            type: 0,
            title: false,
            closeBtn: 0,
            btn: false,
            content: '<div class="layui-progress layui-progress-big" lay-filter="progress" lay-showpercent="true"><div class="layui-progress-bar" lay-percent="0%"></div></div>'
        });


        worker.onmessage = function (event) {
            var workresult = event.data;
            if (workresult.code == 0) {
                var percent = workresult.percent;
                element.progress('progress', percent + '%')
                if (workresult.percent == 100) {
                    layer.close(progressLayer);
                    layer.closeAll();
                    worker.terminate();
                    LoadDetail();
                    OpenSuccessMessage("上传成功");
                }
            } else {
                layer.close(progressLayer);
                layer.closeAll();
                worker.terminate();
                OpenFail("上传失败");
            }

        }
        param.Authorization = GetAuthorization();
        worker.postMessage(param);
    }

    function Stop(){
        var param = {
            projectID: GetUrlParam("projectID")
        }
        var loading = layer.load('停止中...', {
            icon: 16,
            shade: 0.01
        })
        HttpPost(JSON.stringify(param), 'api/depoly/stop', function (result) {
            layer.close(loading);
            if (result.code == 0) {
                OpenSuccessMessage("停止成功")
                LoadDetail();
            } else {
                OpenFail(result.msg);
            }
        })
    }

    function Start(){
        var param = {
              projectID: GetUrlParam("projectID")
        }
        var loading = layer.load('启动中...', {
            icon: 16,
            shade: 0.01
        })
        HttpPost(JSON.stringify(param), 'api/depoly/replaceStart', function (result) {
            layer.close(loading);
            if (result.code == 0) {
                OpenSuccessMessage("启动成功");
                 CloseDialog('replace-success');
            } else {
                OpenFail(result.msg);
            }
        })
    }

    function HandleClose() {
       CloseDialog();
    }


</script>

</html>